﻿using System;
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Transforms;

public class GameOverSystem : SystemBase {
    public EventHandler<EventArgs> OnGameOver;

    private EntityQuery query;

    protected override void OnCreate() {
        base.OnCreate();
        query = GetEntityQuery(ComponentType.ReadOnly<MinoGridEvent>());
    }

    protected override void OnUpdate() {
        var eventTypes = query.ToComponentDataArray<MinoGridEvent>(Allocator.Temp);
        if (eventTypes.Length > 0) { 
            if (eventTypes[0].Value == MinoGridEventType.GameOver){
                OnGameOver?.Invoke(this, EventArgs.Empty);
                Entities.ForEach((ref MinoGridEvent mgEvent) => {
                    mgEvent.Value = MinoGridEventType.Idle;
                }).Run();
            }
        }
        eventTypes.Dispose();
    }
}
